/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY. 
 * See the GNU General Public License for more details.
 * 
 * Copyright (C) 2011 CellObject.net
 */
package net.cellobject.javacodeformatter.handler;

import java.util.Iterator;

import net.cellobject.javacodeformatter.editor.PropertyEditor;
import net.cellobject.javacodeformatter.editor.PropertyElement;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * Handler for deleting selected entries from the Properties editor
 */
public class DeletePropertyHandler extends AbstractHandler {
	public Object execute(final ExecutionEvent event) throws ExecutionException {
		final ISelection selection = HandlerUtil.getCurrentSelection(event);
		if (! (selection instanceof IStructuredSelection)) {
			return null;
		}
		final IEditorPart editor = HandlerUtil.getActiveEditor(event);
		if (! (editor instanceof PropertyEditor)) {
			return null;
		}
		return execute((PropertyEditor)editor, (IStructuredSelection)selection);
	}

	private Object execute(final PropertyEditor editor, final IStructuredSelection selection) {
		// Build an array of properties to be removed.
		final Iterator<?> iter = selection.iterator();
		final int size = selection.size();
		final PropertyElement[] elements = new PropertyElement[size];
		for (int i = 0; i < size; i++) {
			elements[i] = (PropertyElement) (iter.next());
		}
		// Build the operation to be performed.
		final DeletePropertyOperation op = new DeletePropertyOperation(elements);
		op.addContext(editor.getUndoContext());
		// The progress monitor so the operation can inform the user.
		final IProgressMonitor monitor = editor.getEditorSite().getActionBars().getStatusLineManager().getProgressMonitor();
		// An adapter for providing UI context to the operation.
		final IAdaptable info = new IAdaptable() {
			@SuppressWarnings("rawtypes")
			public Object getAdapter(final Class adapter) {
				if (Shell.class.equals(adapter)) {
					return editor.getSite().getShell();
				}
				return null;
			}
		};
		// Execute the operation.
		try {
			editor.getOperationHistory().execute(op, monitor, info);
		} catch (final ExecutionException e) {
			MessageDialog.openError(editor.getSite().getShell(), "Remove Properties Error", "Exception while removing properties: " + e.getMessage());
		}
		return null;
	}
}
